<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>prototype_CIS_525_HW3</title>
</head>
<body>
	<?php
	
		// variables used in script
         $umid = isset($_POST[ "umid" ]) ? $_POST[ "umid" ] : "";
         $fname = isset($_POST[ "fname" ]) ? $_POST[ "fname" ] : "";
         $lname = isset($_POST[ "lname" ]) ? $_POST[ "lname" ] : "";
         $title = isset($_POST[ "title" ]) ? $_POST[ "title" ] : "";
         $email = isset($_POST[ "email" ]) ? $_POST[ "email" ] : "";
         $phone = isset($_POST[ "phone" ]) ? $_POST[ "phone" ] : "";
         
         $iserror = false;
         $formerrors =
            array( "umiderror" => false, "emailerror" => false, "phoneerror" => false );
         
         // array of name values for the text input fields
         $inputlist = array( "umid" => "UMID", "fname" => "First Name",
         		"lname" => "Last Name", "title"=>"Project Title", "email" => "Email",
         		"phone" => "Phone Number" );
	
		if(isset( $_POST["submit"]))
		{
			if(!preg_match("/[0-9]{8}/", $umid))
			{
				$formerrors["umiderror"] = true;
				$iserror = true;
			}
				
			if(!preg_match("/[A-Za-z0-9._%+-]+@[A-Za-z0-9]{1,20}(.[A-Za-z0-9]{1,20}){1,3}/", $email))
			{
				$formerrors["emailerror"] = true;
				$iserror = true; 
			}
				
			if(!preg_match("/[0-9]{3}-[0-9]{3}-[0-9]{4}/", $phone))
			{
				$formerrors["phoneerror"] = true;
				$iserror = true;
			}
		}
		
		if(!$iserror)
		{
			$query = "SELECT * FROM slots";
			
			// Connect to MySQL
			if ( !( $database = mysql_connect(
					"richmondj.coscy9cmelyy.us-east-1.rds.amazonaws.com","jason_richmond", "1fltbal2" ) ) )
				die( "<p>Could not connect to database</p></body></html>" );
			
			// open my database
			if ( !mysql_select_db( "richmondjdb", $database ) )
				die( "<p>Could not open my database</p>
               </body></html>" );
			 
			// query my database
			if ( !( $result = mysql_query( $query, $database ) ) )
			{
				print( "<p>Could not execute query!</p>" );
				die( mysql_error() . "</body></html>" );
			} // end if
		}
		
		if($iserror)
		{
			print( "<p class='error'>Fill in fields with * properly.</p>");
		}
		
		foreach ( $inputlist as $inputname => $inputalt )
		{
			print( "<div><label>$inputalt:</label><input type = 'text'
					name = '$inputname' value = '" . $$inputname . "'>" );
		
			if ( $formerrors[ ( $inputname )."error" ] == true )
				print( "<span class = 'error'>*</span>" );
		
			print( "</div>" );
		} // end foreach

		print("<form method='post' action='index.php'>");
		print("<div><label>UMID:</label><input type='text' name='tbxUmid'>");
		
		if($formerrors["umiderror"])
		{
			print("<p class='error'>UMID needs 8 characters</p>");
		}
		
		if($formerrors["emailerror"])
		{
			print("<p class='error'>Invalid Email</p>");
		}
		
		if($formerrors["phoneerror"] == true)
		{
			print("<p class='error'>Invalid Phone Number</p>");
		}
		
		print("</div>");		
		print("<div><label>Time Slot</label><select>");
		
		// fetch each record in result set
		for ( $counter = 0; $row = mysql_fetch_row( $result );
		++$counter )
		{
			// build table to display results
			print( "<tr>" );
		
			foreach ( $row as $key => $value )
			{ $text = $text." ".$value; }
			 
			$text = $text." seats available";
			print( "<option>$text</option>" );
			$text = "";
		} // end for
		
		print("</select></div>");
		
		print("<div><input type='submit' name='btnSubmit' value='Submit'></div></form>");		
	?>		
</body>
</html>